package com.be.beadmin.product.controller;

import com.alibaba.excel.EasyExcel;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.utils.ResultModel;
import com.be.beadmin.product.dto.input.UserWorkHourInfoDto;
import com.be.beadmin.product.dto.output.UserWorkHourInfoEx;
import com.be.beadmin.product.service.UserWorkHourInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
* @author zjm
* @date 2023-11-22
**/
@Api(tags = "用户的工时")
@RestController
public class UserWorkHourInfoController {

    @Autowired
    private  UserWorkHourInfoService userWorkHourInfoService;

    @ApiOperation(value = "获取当前用户的工序计时信息")
    @PostMapping(value = "/product/getUserWorkHourInfo")
    public ResultModel getUserWorkHourInfo(HttpServletRequest request, @RequestParam String processId)  {
        return ResultModel.success(userWorkHourInfoService.getUserWorkHourInfo(request,processId)) ;
    }
    @PostMapping(value = "/product/getUserWorkHourInfoByPage")
    public ResultModel getUserWorkHourInfoByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(userWorkHourInfoService.getUserWorkHourInfoByPage(pageEntity)) ;
    }

    @PostMapping("/product/addUserWorkHourInfo")
    public ResultModel addUserWorkHourInfo(@RequestBody UserWorkHourInfoDto userWorkHourInfoDto){
        userWorkHourInfoService.addUserWorkHourInfo(userWorkHourInfoDto);
        return ResultModel.success(ResultModel.ADD);
    }

    @PostMapping("/product/delUserWorkHourInfo")
    public ResultModel delUserWorkHourInfo(@RequestBody UserWorkHourInfoDto userWorkHourInfoDto){
        userWorkHourInfoService.delUserWorkHourInfo(userWorkHourInfoDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }


    @PostMapping("/product/updateUserWorkHourInfo")
    public ResultModel updateUserWorkHourInfo(@RequestBody UserWorkHourInfoDto userWorkHourInfoDto){
        userWorkHourInfoService.updateUserWorkHourInfo(userWorkHourInfoDto);
        return ResultModel.success(ResultModel.UPDATE);
    }


    @PostMapping("/product/exportUserWorkHourInfo")
    public void exportUserWorkHourInfo(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<UserWorkHourInfoEx> userWorkHourInfoPage = userWorkHourInfoService.getUserWorkHourInfoByPage(pageEntity);
        List<UserWorkHourInfoEx> userWorkHourInfoExes = userWorkHourInfoPage.getContent();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("UserWorkHourInfo接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), UserWorkHourInfoEx.class).sheet("sheet1").doWrite(userWorkHourInfoExes);
    }
}